package com.ruoyi.project.order.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.OrderCheckStatu;
import com.ruoyi.common.constant.OrderStatu;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.order.domain.Customer;
import com.ruoyi.project.order.domain.OrderType;
import com.ruoyi.project.order.domain.WorkOrder;
import com.ruoyi.project.order.mapper.WorkOrderMapper;
import com.ruoyi.project.order.service.ICustomerService;
import com.ruoyi.project.order.service.IOrderTypeService;
import com.ruoyi.project.order.service.IWorkOrderService;
import com.ruoyi.project.order.vo.OrderListVo;
import com.ruoyi.project.order.vo.SelectOrderVo;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author L
 * @since 2020-08-07
 */
@Controller
@RequestMapping("/order_my")
@Slf4j
public class OrderMyController extends  BaseController {
    @Autowired
    ICustomerService iCustomerService;
    @Autowired
    IWorkOrderService iWorkOrderService;
    @Autowired
    IOrderTypeService iOrderTypeService;
    @Autowired
    IUserService iUserService;
    @Autowired
    WorkOrderMapper workOrderMapper;

    private String prefix = "order/my";

    @GetMapping()
    public String orderMy(Model model) {
        return prefix + "/order_my";
    }

    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


    /**
     * 新增保存客户
     */
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Customer customer) {
        //查询客户是否存在
        Customer rCustomer = iCustomerService.getOne(new QueryWrapper<Customer>().lambda()
                .eq(Customer::getCustomerName, customer.getCustomerName().trim())
                .eq(Customer::getCustomerPhone, customer.getCustomerPhone().trim())
                .eq(Customer::getCustomerAddress, customer.getCustomerAddress().trim())
        );
        if (rCustomer == null) {
            rCustomer = customer;
            log.info("添加客户: " + rCustomer);
        } else {
            return error("修改失败,用户已存在");
        }
        return toAjax(iCustomerService.save(rCustomer));

    }


    /**
     * 修改保存客户
     */
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Customer customer) {
        //查询客户是否存在
        Customer rCustomer = iCustomerService.getOne(new QueryWrapper<Customer>().lambda()
                .eq(Customer::getCustomerName, customer.getCustomerName().trim())
                .eq(Customer::getCustomerPhone, customer.getCustomerPhone().trim())
                .eq(Customer::getCustomerAddress, customer.getCustomerAddress().trim())
        );
        if (rCustomer == null) {
            rCustomer = customer;
            log.info("添加客户: " + rCustomer);
        } else {
            return error("修改失败,用户已存在");
        }
        return toAjax(iCustomerService.updateById(rCustomer));
    }


    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(SelectOrderVo selectOrderVo) {
        startPage();
        //判断身份
        switch (getRole()) {
            case publisher:
                selectOrderVo.setPublisherId(getUserId().intValue());
                break;
            case worker:
                selectOrderVo.setWorkerId(getUserId().intValue());
                break;
            case checker:
                selectOrderVo.setCheckerId(getUserId().intValue());
                break;
        }
        List<OrderListVo> orderListVos = null;
        //如果有检测逾期
        System.out.println(selectOrderVo);
        if (StrUtil.isNotBlank(selectOrderVo.getTimeOut())) {
            orderListVos =  workOrderMapper.LGetListNoContentAndYuQi(selectOrderVo);
        }else{
            orderListVos = workOrderMapper.LGetListNoContent(selectOrderVo);
        }
        return getDataTable(orderListVos);
    }


    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            for (String id : ids.split(",")) {
                iCustomerService.removeById(Integer.valueOf(id));
            }
            return toAjax(true);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }



}
